﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Resources;
using WebCrawler.ProcessingFiles.CategoryClasses;


namespace WebCrawler.ProcessingFiles
{
    public partial class RunProcess : System.Web.UI.Page
    {
        
        NewEggClasses vendor = new NewEggClasses();
        int vendorID = 1;
        
        Exception ex = new Exception();
        public String errorPage = "ProcessProducts";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                try
                {

                    ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                    var query = from c in dc.Categories where c.process == 0 && c.exclude == 0 select c;

                    if (query.Count() > 0)
                    {

                        Response.Redirect("GrabPage.aspx");

                    }
                    else
                    {

                        ProductDataDataContextDataContext dc2 = new ProductDataDataContextDataContext();
                        var query2 = from b in dc2.Brands where b.process == 0 select b;
                        if (query2.Count() > 0)
                        {

                            Response.Redirect("ProcessBrands.aspx");

                        }
                        else
                        {
                            ProductDataDataContextDataContext dc1 = new ProductDataDataContextDataContext();

                            var query1 = from pd in dc1.ProductLinks where pd.process == 0 select pd;
                            if (query1.Count() > 0)
                            {

                                Response.Redirect("ProcessProducts.aspx");

                            }
                            else
                            {
                                StartProcess();
                                GetTopLevelCategoryLinks("MAIN");
                                ProcessCategories();
                                
                            }
                        }
                    }
                }
                catch(Exception ex) 
                {
          

                      ProcessErrorText(ex);

                }
               
            }

            if (Request.QueryString.Count > 0)
            {
                ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();
                
                    string linktext = "";
                    string linkurl = "";
                    string catid = "";

                    if (Request.QueryString.AllKeys.Contains("link"))
                    {
                        linktext = Request.QueryString["link"].ToString().Trim();
                    }
                    if (Request.QueryString.AllKeys.Contains("url"))
                    {
                        linkurl = Request.QueryString["url"].ToString().Trim();
                    }
                    if (Request.QueryString.AllKeys.Contains("catid"))
                    {
                        catid = Request.QueryString["catid"].ToString().Trim();
                    }
                    string URL = linkurl;
                    string Title = Server.HtmlEncode(linktext);
                    string LinkText = Server.HtmlEncode(linktext);
                    string CatID = Server.HtmlEncode(catid);

                    if (URL.Length > 0 && LinkText.Length > 0)
                    {

                        if (URL.Contains(","))
                        {
                            char[] separator = new char[] { ',' };

                            string[] strSplitArr = URL.Split(separator);

                            if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                            {

                                URL = strSplitArr[0];
                            }


                        }

                        if (LinkText.Contains(","))
                        {
                            char[] separator = new char[] { ',' };

                            string[] strSplitArr = LinkText.Split(separator);

                            if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                            {

                                LinkText = strSplitArr[0].ToString().Trim();
                            }


                        }

                        if (Title.Contains(","))
                        {
                            char[] separator = new char[] { ',' };

                            string[] strSplitArr = Title.Split(separator);

                            if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                            {
                                Title = strSplitArr[0].ToString().Trim();
                            }

                        }

                        if (Title.Contains("undefined"))
                        {
                            Title = "";
                        }

                        if (CatID.Contains(","))
                        {
                            char[] separator = new char[] { ',' };

                            string[] strSplitArr = CatID.Split(separator);

                            if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                            {
                                CatID = strSplitArr[0].ToString().Trim();
                            }

                        }

                        if (vendor.VendorName == "NewEgg")
                        {

                            ProceesNewEgg(URL, Title, LinkText, CatID);

                        }
                    }
               
            }



        }

        protected void ProceesNewEgg(string url, string title, string linktext, string catid)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


                string URL = url;
                string Title = title;
                string LinkText = linktext;
                string CatID = catid;

               

                if (URL.Contains("/Store/"))
                {
                    if (URL.Contains("SubCategory="))
                    {
                        ProcessLinks(LinkText, URL, 3);

                    }
                    else if (URL.Contains("Category="))
                    {
                        ProcessLinks(LinkText, URL, 2);

                    }
                    else if (URL.Contains("Brand="))
                    {
                        ProcessBrands(LinkText, URL, LinkText);

                    }
                    else
                    {
                        if (!LinkText.Contains("shop All"))
                        {
                            ProcessLinks(LinkText, URL, 1);
                        }
                    }
                }

                    if (URL.Contains("Item=") && !LinkText.Contains("nbsp;") && LinkText.Length>0)
                    {
                        ProcessLinks(LinkText, URL, CatID);
                    }

        }

        protected void ProcessCategories()
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();
            ProductDataDataContextDataContext dc2 = new ProductDataDataContextDataContext();
            ProductDataDataContextDataContext dc3 = new ProductDataDataContextDataContext();

            

                string[] MainCats = vendor.MainCats;
                foreach (var cm in MainCats)
                {
                    var qry = from ct in dc.Categories where ct.level == 1 && ct.catURL.Contains("name=" + cm.Trim()) select ct;
                    if (qry.Count() == 1)
                    {
                       
                            var item = qry.Single();

                            item.vendorID = vendorID;
                            item.process = 1;
                            dc.SubmitChanges();
                       
                    }
                }
               
        }


        

       

        protected void ProcessLinks(string linktext, string linkurl, int level)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

          
       
                string LinkText = linktext.Trim();
                string LinkURL = linkurl.Trim();
                int Level = level;

                var query = from ct in dc.Categories where ct.catName == LinkText select ct;

                if (query.Count() == 0)
                {
                    Category cat = new Category();
                    cat.catName = LinkText;
                    cat.catURL = LinkURL.Replace("|","&");
                    cat.catShortDesc = LinkText;
                    cat.level = Level;
                    cat.process = 0;
                    cat.vendorID = vendorID;
                    cat.exclude = 0;

                    dc.Categories.InsertOnSubmit(cat);
                    dc.SubmitChanges();
                   
                }
        }

        protected void ProcessLinks(string linktext, string linkurl, string catid)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


            string LinkText = linktext.Trim();
            string LinkURL = linkurl.Trim();
            LinkURL = LinkURL.Replace("|", "&");

            string CatID = catid.Trim();
            int CatId = 0;

           

            if (!string.IsNullOrEmpty(CatID) && Int32.TryParse(CatID, out CatId))
            {
                CatId = int.Parse(CatID);
            }
            



            var query = from pl in dc.ProductLinks where pl.productName == LinkText select pl;

            if (query.Count() == 0)
            {
                ProductLink p = new ProductLink();
                p.productName = LinkText;
                p.productURL = LinkURL;
                p.process = 0;
                p.vendorID = vendorID;
                p.catID = CatId;
               


                dc.ProductLinks.InsertOnSubmit(p);
                dc.SubmitChanges();
            }


        }

      

        protected void ProcessBrands(string linktext, string linkurl, string linktitle)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


                string LinkText = linktext.Trim();
                string LinkURL = linkurl.Trim();
                LinkURL = LinkURL.Replace("|", "&");

                if (LinkText.Length == 0)
                {
                    int a = LinkURL.IndexOf("name=");
                    int b = LinkURL.Length;
                    int c = a + 5;

                    LinkText = LinkURL.Substring(c, b - c);
                }

                var query = from b in dc.Brands where b.brandName == LinkText select b;

                if (query.Count() == 0)
                {
                    Brand b = new Brand();
                    b.brandName= Server.HtmlEncode(LinkText);
                    b.brandURL = LinkURL;
                    b.process = 0;

                    dc.Brands.InsertOnSubmit(b);
                    dc.SubmitChanges();
                }

        }

        
        protected void StartProcess()
        {

            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

            var query = from c in dc.Categories where c.process == 1 select c;
            
            if (query.Count() > 0)
            {
                foreach (var item in query)
                {
                    item.process = 0;
                    dc.SubmitChanges();
                }
            }


            ProductDataDataContextDataContext dc1 = new ProductDataDataContextDataContext();

            var query1 = from b in dc1.Brands where b.process == 1 select b;

            if (query1.Count() > 0)
            {
                foreach (var item in query1)
                {
                    item.process = 0;
                    dc1.SubmitChanges();
                }
            }

            ProductDataDataContextDataContext dc2 = new ProductDataDataContextDataContext();

            var query2 = from pl in dc2.ProductLinks where pl.process == 1 select pl;

            if (query2.Count() > 0)
            {
                foreach (var item in query2)
                {
                    item.process = 0;
                    dc2.SubmitChanges();
                }
            }

            ProductDataDataContextDataContext dc3 = new ProductDataDataContextDataContext();

            var query3 = from pl in dc2.ProductLinks where pl.catID ==0 || pl.productName =="Predict" select pl;

            if (query3.Count() > 0)
            {
                foreach (var item in query3)
                {
                   
                    var q = (from pp in dc.PredictProducts where pp.predictURL == item.productURL select pp).FirstOrDefault();

                    if (q != null)
                    {
                        q.predictResult = 2;
                        dc3.SubmitChanges();

                    }

                    dc3.ProductLinks.DeleteOnSubmit(item);
                    dc3.SubmitChanges();
                }
            }


        }

        protected void GetTopLevelCategoryLinks(string URL)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                string CurrURL = "";

                if (URL == "MAIN")
                {

                    CurrURL = vendor.MainLink;
                }
                else
                {

                    CurrURL = URL;
                }

                string strResult = "";

                WebResponse objResponse;
                WebRequest objRequest = System.Net.HttpWebRequest.Create(CurrURL);

                objResponse = objRequest.GetResponse();

                using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                {
                    strResult = sr.ReadToEnd();
                    sr.Close();
                }

                strResult = strResult.Replace("\"", "");
                strResult = strResult.Replace("#", "");
                strResult = strResult.Replace("&", "|");
                strResult = strResult.Remove(0, 10000);

                Response.Write("<p class='URLS'>");
                Response.Write("<div class='showtext' style='display:none'>");
                Response.Write(strResult);
                Response.Write("</div>");

        }



        protected void ProcessErrorText(Exception ex)
        {

            StringBuilder errorText = new StringBuilder();

            if (ex != null)
            {
                if (ex.Source != null)
                {
                    errorText.Append(errorPage + " :source: " + ex.Source);
                }
                if (ex.Message != null)
                {
                    errorText.Append(errorPage + " :message: " + ex.Message);
                }
                if (ex.InnerException != null)
                {
                    errorText.Append(errorPage + " :innerexception: " + ex.InnerException);
                }
                if (ex.TargetSite != null)
                {
                    errorText.Append(errorPage + " :target: " + ex.TargetSite);
                }

            }
            else
            {
                errorText.Append("unknown error");
            }

            ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();

            WebCrawler wc = new WebCrawler();
            wc.webCrawlerErrorText = errorText.ToString();

            dce.WebCrawlers.InsertOnSubmit(wc);
            dce.SubmitChanges();
        }

        protected void GetNextPage(object sender, EventArgs e)
        {
            Response.Redirect("/ProcessingFiles/RedirectForm.aspx?Type=1&Name=Main");
        }
    }
}